package com.cqhilink.iot.jobstandardmanagement.services.innerservice.employee.util;

import com.cqhilink.iot.jobstandardmanagement.common.bean.ApplicationContextHelper;
import com.cqhilink.iot.jobstandardmanagement.facade.model.Employee;
import com.cqhilink.iot.jobstandardmanagement.facade.webservicesmodel.UasUser;
import com.cqhilink.iot.jobstandardmanagement.facade.webservicesmodel.UasUserExt;
import com.cqhilink.iot.jobstandardmanagement.facade.service.employee.JsmEmployeeService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/2/15.
 */
public class EmployeePool implements Runnable {
    private String controlType;//1.添加，2.修改，3.删除
    private List<UasUserExt> uueList;//用户扩展信息
    private List<UasUser> uuList;//用户信息
    private String type;

    public void setControlType(String controlType) {
        this.controlType = controlType;
    }

    public void setUueList(List<UasUserExt> uueList) {
        this.uueList = uueList;
    }

    public void setUuList(List<UasUser> uuList) {
        this.uuList = uuList;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public void run() {
        JsmEmployeeService employeeService = (JsmEmployeeService) ApplicationContextHelper.getBean("employeeService");
        if (employeeService != null && StringUtils.isNotEmpty(controlType)) {
            List<Employee> paramList = new ArrayList<Employee>();
            try {
                employee(uuList, paramList);
                uasUserExt(uueList, paramList, controlType);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (paramList.size() > 0) {
                if ("3".equals(type)) {//处理用户信息
                    if ("1".equals(controlType)) {
                        //添加
                        employeeService.insertList(paramList);
                    } else if ("2".equals(controlType)) {
                        //修改
                        employeeService.updateList(paramList);
                    } else if ("3".equals(controlType)) {
                        //删除
                        employeeService.deleteList(paramList);
                    }
                } else if ("4".equals(type)) {//处理用户扩展信息
                    List<String> strList = employeeService.selectByListId(paramList);
                    if (strList != null && strList.size() > 0) {
                        List<Employee> updateList = new ArrayList<Employee>();
                        for (Employee e : paramList) {
                            if (strList.contains(e.getEmployeeId())) {
                                updateList.add(e);
                            }
                        }
                        employeeService.updateList(updateList);
                    }
                }
            }
        }

    }

    /**
     * 处理用户信息
     *
     * @param list
     * @param paramList
     * @throws Exception
     */
    private void employee(List<UasUser> list, List<Employee> paramList) throws Exception {
        if (list != null && list.size() > 0) {
            ConvertUtils.register(new DateConverter(null), Date.class);
            for (UasUser uu : list) {
                Employee param = new Employee();
                BeanUtils.copyProperties(param, uu);
                param.setEmployeeId(uu.getUserId());
                param.setPostId("1");
                param.setSex((short) 0);
                param.setOperatorId("1");
                param.setOrgId("");
                param.setDepId("");
                param.setCardId("");
                param.setPortraitCode("");
                param.setMobilePhone("");
                param.setOfficeTel("");
                param.setAddress("");
                param.setPostcode("");
                param.setQqid("");
                param.setEmail("");
                param.setEmgContactPerson("");
                param.setEmgContactTel("");
                param.setDescription("");
                param.setName(uu.getUserName());
                param.setWorkNumber("1");
                param.setCreatorId("1");
                param.setCreateTime(new Date());
                param.setModifierId("1");
                param.setModifyTime(new Date());
                paramList.add(param);
            }
        }
    }

    /**
     * 处理用户扩展信息
     *
     * @param list
     * @param paramList
     * @param controlType
     * @throws Exception
     */
    private void uasUserExt(List<UasUserExt> list, List<Employee> paramList, String controlType) throws Exception {
        if (list != null && list.size() > 0) {
            ConvertUtils.register(new DateConverter(null), Date.class);
            if ("3".equals(controlType)) {
                for (UasUserExt uue : list) {
                    Employee param = new Employee();
                    param.setEmployeeId(uue.getUserId());
                    param.setSex((short) 0);
                    param.setUserType("");
                    param.setEmail("");
                    param.setDescription("");
                    param.setModifierId(uue.getModifier());
                    param.setModifyTime(uue.getModifyDate());
                    paramList.add(param);
                }
            } else {
                for (UasUserExt uue : list) {
                    Employee param = new Employee();
                    BeanUtils.copyProperties(param, uue);
                    if (StringUtils.isEmpty(uue.getSex())) {
                        param.setSex((short) 0);
                    }
                    if (StringUtils.isEmpty(uue.getMemo())) {
                        param.setDescription("");
                    } else {
                        param.setDescription(uue.getMemo());
                    }
                    param.setEmployeeId(uue.getUserId());
                    param.setModifierId(uue.getModifier());
                    param.setModifyTime(uue.getModifyDate());
                    paramList.add(param);
                }
            }
        }
    }

}
